import axios from './request.js'

let oParams = {}
location.search.slice(1).split('&').forEach((item) => {
    let _arr = item.split('=')
    let k = _arr[0]
    oParams[k] = _arr[1]
})



function WxLogin() {
  const i = location.href.indexOf('?')
  const _url = location.href.slice(0, i)
  let url = encodeURI(
    `https://tiku.tibangbang.cn/question-2c-wx/wx_login.html?to_path=${_url}`
  )
  const href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf338a81c8c50bf2e&redirect_uri=${url}&response_type=code&scope=snsapi_base&state=${oParams.openid}#wechat_redirect`
  // const href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf338a81c8c50bf2e&redirect_uri=${url}&response_type=code&scope=snsapi_userinfo&state=${oParams.openid}#wechat_redirect`

  location.href = href
}

export default function login() {
  return new Promise((resolve, reject) => {
    if (!oParams.openid) {
      alert('该网页链接有问题！请通过公众号下方菜单获取新的网页链接~~')
      return
    }
    axios.get('/getUserByOpenid').then(res => {
      resolve(res.data.data)
    }).catch(err => {
      WxLogin()
    })
  })
} 